#include <cubefs/api/api.hpp>
#include <natives.h>
#include <decls.hpp>
#include <exceptions.hpp>

#include <cubefs/core/tools/types/dispatcher.hpp>

#define CUBEFS_SOURCE "value.cpp:" CUBEFS_AT

using namespace cubefs::api;
using namespace cubefs::tools;

using namespace std;

JNIEXPORT void JNICALL VALUE_PKG(Value, SetManaged) (JNIEnv *env, jclass, jlong handle, jboolean managed) {
    CUBEFS_EXC_BEGIN

    Value* value = Handle2Ptr<Value>(handle);
    value->SetManaged(managed);

    CUBEFS_EXC_HANDLE
}

JNIEXPORT jboolean JNICALL VALUE_PKG(Value, IsEquals) (JNIEnv *env, jclass, jlong handle, jlong other) {
    CUBEFS_EXC_BEGIN

    Value* value = Handle2Ptr<Value>(handle);
    Value* other_value = Handle2Ptr<Value>(other);
    return value->IsEquals(other_value);

    CUBEFS_EXC_HANDLE
    return 0;
}

JNIEXPORT void JNICALL VALUE_PKG(Value, Set) (JNIEnv *env, jclass, jlong handle, jlong other) {
    CUBEFS_EXC_BEGIN

    Value* value = Handle2Ptr<Value>(handle);
    Value* other_value = Handle2Ptr<Value>(other);
    value->Set(other_value);

    CUBEFS_EXC_HANDLE
}

JNIEXPORT jboolean JNICALL VALUE_PKG(BooleanValue, GetValue) (JNIEnv *env, jclass, jlong handle) {
    CUBEFS_EXC_BEGIN

    BooleanValue* value = Handle2Ptr<BooleanValue>(handle);
    return value->GetValue();

    CUBEFS_EXC_HANDLE
    return 0;
}

JNIEXPORT void JNICALL VALUE_PKG(BooleanValue, SetValue) (JNIEnv *env, jclass, jlong handle, jboolean value) {
    CUBEFS_EXC_BEGIN

    BooleanValue* value = Handle2Ptr<BooleanValue>(handle);
    return value->SetValue(value);

    CUBEFS_EXC_HANDLE
}


JNIEXPORT jstring JNICALL VALUE_PKG(ConstStringValue, GetValue) (JNIEnv *env, jclass, jlong handle) {
    CUBEFS_EXC_BEGIN

    ConstStringValue* value = Handle2Ptr<ConstStringValue>(handle);
    return env->NewStringUTF(value->GetValue().c_str());

    CUBEFS_EXC_HANDLE
    return 0;
}

JNIEXPORT jstring JNICALL VALUE_PKG(StringRefValue, GetValue) (JNIEnv *env, jclass, jlong handle) {
    CUBEFS_EXC_BEGIN

    StringRefValue* value = Handle2Ptr<StringRefValue>(handle);
    return env->NewStringUTF(value->GetValue().c_str());

    CUBEFS_EXC_HANDLE
    return 0;
}

JNIEXPORT jstring JNICALL VALUE_PKG(StringValue, GetValue) (JNIEnv *env, jclass, jlong handle) {
    CUBEFS_EXC_BEGIN

    StringValue* value = Handle2Ptr<StringValue>(handle);
    return env->NewStringUTF(value->GetValue().c_str());

    CUBEFS_EXC_HANDLE
    return 0;
}

JNIEXPORT void JNICALL VALUE_PKG(StringValue, SetValue) (JNIEnv *env, jclass, jlong handle, jstring string) {
    CUBEFS_EXC_BEGIN

    StringValue* value = Handle2Ptr<StringValue>(handle);
    const char* str = env->GetStringUTFChars(string, NULL);
    if (str != NULL)
    {
    	value->SetValue(str);
    	env->ReleaseStringUTFChars(string, str);
    }

    CUBEFS_EXC_HANDLE
}



JNIEXPORT jstring JNICALL VALUE_PKG(VersionValue, GetValue) (JNIEnv *env, jclass, jlong handle) {
    CUBEFS_EXC_BEGIN

    VersionValue* value = Handle2Ptr<VersionValue>(handle);
    return env->NewStringUTF(value->GetValue().str().c_str());

    CUBEFS_EXC_HANDLE
    return 0;
}

JNIEXPORT void JNICALL VALUE_PKG(VersionValue, SetValue) (JNIEnv *env, jclass, jlong handle, jstring string) {
    CUBEFS_EXC_BEGIN

    VersionValue* value = Handle2Ptr<VersionValue>(handle);
    const char* str = env->GetStringUTFChars(string, NULL);
    if (str != NULL)
    {
    	value->SetValue(Version(str));
    	env->ReleaseStringUTFChars(string, str);
    }

    CUBEFS_EXC_HANDLE
}

JNIEXPORT jlong JNICALL VALUE_PKG(ConstVoidPtrValue, GetValue) (JNIEnv *env, jclass, jlong handle) {
    CUBEFS_EXC_BEGIN

    ConstVoidPtrValue* value = Handle2Ptr<ConstVoidPtrValue>(handle);
    return Ptr2Handle(value->GetValue());

    CUBEFS_EXC_HANDLE
    return 0;
}


JNIEXPORT jlong JNICALL VALUE_PKG(VoidPtrValue, GetValue) (JNIEnv *env, jclass, jlong handle) {
    CUBEFS_EXC_BEGIN

    VoidPtrValue* value = Handle2Ptr<VoidPtrValue>(handle);
    return Ptr2Handle(value->GetValue());

    CUBEFS_EXC_HANDLE
    return 0;
}

JNIEXPORT void JNICALL VALUE_PKG(VoidPtrValue, SetValue) (JNIEnv *env, jclass, jlong handle, jlong value_handle) {
    CUBEFS_EXC_BEGIN

    VoidPtrValue* value = Handle2Ptr<VoidPtrValue>(handle);
    value->SetValue(Handle2Ptr<void>(value_handle));

    CUBEFS_EXC_HANDLE
}

#undef CUBEFS_SOURCE
